using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LS.UnityExtensions;

namespace LS.DataTable
{
    [Serializable]
    public class DataTableDefaultSetting
    {
        [TypeSearch(typeof(ISceneData), "SceneDataType")]
        public string SceneDataTypeName = DataTableExtension.DefaultDataTypeMap["Scene"];

        [TypeSearch(typeof(IEntityData), "EntityDataType")]
        public string EntityDataTypeName = DataTableExtension.DefaultDataTypeMap["Entity"];

        [TypeSearch(typeof(IMusicData), "MusicDataType")]
        public string MusicDataTypeName = DataTableExtension.DefaultDataTypeMap["Music"];

        [TypeSearch(typeof(ISoundData), "SoundDataType")]
        public string SoundDataTypeName = DataTableExtension.DefaultDataTypeMap["Sound"];

        [TypeSearch(typeof(IUIFormData), "UIFormDataType")]
        public string UIFormDataTypeName = DataTableExtension.DefaultDataTypeMap["UIForm"];

        [TypeSearch(typeof(IUISoundData), "UISoundDataType")]
        public string UISoundDataTypeName = DataTableExtension.DefaultDataTypeMap["UISound"];

        public bool SceneAvailable => !string.IsNullOrEmpty(SceneDataTypeName);
        public bool EntityAvailable => !string.IsNullOrEmpty(EntityDataTypeName);
        public bool MusicAvailable => !string.IsNullOrEmpty(MusicDataTypeName);
        public bool SoundAvailable => !string.IsNullOrEmpty(SoundDataTypeName);
        public bool UIFormAvailable => !string.IsNullOrEmpty(UIFormDataTypeName);
        public bool UISoundAvailable => !string.IsNullOrEmpty(UISoundDataTypeName);

        private Type m_SceneDataType;
        private Type m_EntityDataType;
        private Type m_MusicDataType;
        private Type m_SoundDataType;
        private Type m_UIFormDataType;
        private Type m_UISoundDataType;

        public Type SceneDataType
        {
            get
            {
                if (!SceneAvailable) return null;
                if (m_SceneDataType == null)
                    m_SceneDataType = Type.GetType(SceneDataTypeName, true, false);
                return m_SceneDataType;
            }
        }

        public Type EntityDataType
        {
            get
            {
                if (!EntityAvailable) return null;
                if (m_EntityDataType == null)
                    m_EntityDataType = Type.GetType(EntityDataTypeName, true, false);
                return m_EntityDataType;
            }
        }

        public Type MusicDataType
        {
            get
            {
                if (!MusicAvailable) return null;
                if (m_MusicDataType == null)
                    m_MusicDataType = Type.GetType(MusicDataTypeName, true, false);
                return m_MusicDataType;
            }
        }

        public Type SoundDataType
        {
            get
            {
                if (!SoundAvailable) return null;
                if (m_SoundDataType == null)
                    m_SoundDataType = Type.GetType(SoundDataTypeName, true, false);
                return m_SoundDataType;
            }
        }

        public Type UIFormDataType
        {
            get
            {
                if (!UIFormAvailable) return null;
                if (m_UIFormDataType == null)
                    m_UIFormDataType = Type.GetType(UIFormDataTypeName, true, false);
                return m_UIFormDataType;
            }
        }

        public Type UISoundDataType
        {
            get
            {
                if (!UISoundAvailable) return null;
                if (m_UISoundDataType == null)
                    m_UISoundDataType = Type.GetType(UISoundDataTypeName, true, false);
                return m_UISoundDataType;
            }
        }

        public Type GetTypeByName(string name)
        {
            switch (name)
            {
                case "Scene": return SceneDataType;
                case "Entity": return EntityDataType;
                case "Music": return MusicDataType;
                case "Sound": return SoundDataType;
                case "UIForm": return UIFormDataType;
                case "UISound": return UISoundDataType;
                default: return null;
            }
        }

        public string GetTypeStringByName(string name)
        {
            switch (name)
            {
                case "Scene": return SceneDataTypeName;
                case "Entity": return EntityDataTypeName;
                case "Music": return MusicDataTypeName;
                case "Sound": return SoundDataTypeName;
                case "UIForm": return UIFormDataTypeName;
                case "UISound": return UISoundDataTypeName;
                default: return string.Empty;
            }
        }

        public bool ContainsDataName(string name)
        {
            return DataTableExtension.DefaultDataNames.Contains(name);
        }
    }
}
